<template>
  <!-- // 老师查看学生作业列表 -->
  <div class="tea-joblist swipe-wrapper" slot="content">
    <h1 class="title">学情管理--作业列表</h1>
    <ul v-if="jobs.length>0">
      <li v-for="(job,index) in jobs" :key="index">
        <div @click="onItemClick(job, index)" class="item-inner">
          <div class="text">
            <p class="item-time">
              <span class="item-title">作业内容:</span>
              &nbsp;{{job.bookName}}--{{job.pageNums}}
            </p>
            <p class="item-time">
              <span class="item-title">截止日期:</span>
              &nbsp;{{job.deadline | formatDate}}
            </p>
            <p class="item-time">
              <span class="item-title">班级名称:</span>&nbsp;
              <!-- <span v-for="(grade,index) in job.gradeNames" :key="index">{{grade}}&nbsp;</span> -->
              <span>{{job.grade}}&nbsp;</span>
            </p>
          </div>
          <div class="icon">
            <p class="item-status item-teaundo" v-if="job.status=='0'">未完成批阅</p>
            <p class="item-status item-alldone" v-else>已完成批阅</p>
          </div>
        </div>
      </li>
    </ul>
    <div class="tips" v-else>
      <div class="tips-icon">
        <i class="cubeic-alert"></i>
      </div>
      <h3>您还未发布作业，请移步PC端发布作业！</h3>
    </div>
  </div>
</template>

<script>
import { formatDate } from "@/assets/js/date.js";
export default {
  name: "TeaJobList",
  data() {
    return {
      jobs: []
    };
  },
  computed: {},
  filters: {
    formatDate(time) {
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    }
  },
  created() {
    if (localStorage.getItem("openID")) {
      this.getJobs();
    } else {
      this.getCode();
    }
  },
  mounted() {},
  methods: {
    getUrlKey(name) {
      //获取url 参数
      return (
        decodeURIComponent(
          (new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(
            location.href
          ) || [, ""])[1].replace(/\+/g, "%20")
        ) || null
      );
    },
    getCode() {
      const code = this.getUrlKey("code"); // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
      const local = window.location.href;
      const appid = "wx6d7dcd07ca4fa211";
      if (code == null || code === "") {
        window.location.href =
          "http://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
          appid +
          "&redirect_uri=" +
          encodeURIComponent(local) +
          "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
      } else {
        this.getOpenId(code); //把code传给后台获取用户信息
      }
    },
    async getOpenId(code) {
      // 通过code获取 openId等用户信息，/oauth2/printOpenID 为后台接口
      await this.$http
        .post("/oauth2/printOpenID", { code: code })
        .then(res => {
          let openID = res.data;
          this.$store.state.openID = openID;
          localStorage.setItem("openID", openID);
          this.getJobs();
        })
        .catch(error => {
          console.log(error);
        });
    },

    async getJobs() {
      // 获取老师发布的作业列表
      await this.$http
        .post("/api000020", { openId: localStorage.getItem("openID") })
        .then(response => {
          if (response.data.status === "fail") {
            this.$createToast({
              type: "loading",
              time: 2000,
              txt: "您未绑定教师信息，即将跳转到绑定页面",
              onTimeout: () => {
                this.$router.push({ path: "/teaadd" });
              }
            }).show();
          } else {
            this.jobs = response.data;
          }
        })
        .catch(error => {
          console.log(error);
          // this.$createToast({
          //   type: "warn",
          //   time: 1000,
          //   txt: error
          // }).show();
        });
    },
    onItemClick(item) {
      this.$store.state.homeworkId = item.id;
      this.$router.push({
        path: "/stulist",
        query: item
      });
    }
  }
};
</script>

<style lang="scss">
.tea-joblist {
  .title {
    font-size: 16px;
    font-weight: 700;
    padding-left: 20px;
    height: 50px;
    line-height: 50px;
    border-left: 5px solid #fc9153;
    background: #fee9dd;
  }
  /* 设置列表样式 */
  .item-inner {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    margin: 20px;
    padding-bottom: 20px;
    height: 60px;
    border-bottom: 5px solid rgb(233, 233, 233);
    /* 老师作业列表或学生作业列表每个作业信息框的样式 */
    .text {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .item-title {
        font-weight: 700;
      }
    }
    /* 老师作业列表或学生作业列表每个作业信息框右侧状态或图标样式 */
    .icon {
      align-self: center;
      display: flex;
      flex-direction: column;
      align-items: center;
      .item-teaundo {
        color: red;
      }
      /* /已完成字体样式 */
      .item-alldone {
        color: black;
        text-align: right;
      }
    }
  }
  .tips {
    margin-top: 40%;
    text-align: center;
    font-size: 16px;
    color: #fc9153;
    .tips-icon {
      margin-bottom: 20px;
      i {
        color: #fc9153;
        font-size: 50px;
      }
    }
  }
}
</style>